package com.webapp.loader;

import com.webapp.module.MailSubscriber;
import com.webapp.module.Report;
import com.webapp.util.BaseLog;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class XmlReportsLoader extends BaseLog
{
  private String fileName = "config.xml";
  private Element root = null;

  private Map<String, Report> reportList = new HashMap<String,Report>();

  private static String C8_URL = null;
  private static String C8_NAMESPACE = null;
  private static String C8_USERNAME = null;
  private static String C8_PASSWORD = null;

  private static String C_VERSION = null;
  private static String REPORT_ROOT = null;

  private static String MAIL_HOST = null;
  private static String MAIL_USER = null;
  private static String MAIL_PWD = null;

  public XmlReportsLoader() {
    SAXBuilder shipAlert = new SAXBuilder();
    try {
      InputStream ips = XmlReportsLoader.class.getClassLoader().getResourceAsStream(this.fileName);
      try {
		this.root = shipAlert.build(ips).getRootElement();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    } catch (JDOMException e) {
      logger.error("Load Report List xml error:" + e);
      
    }
  }

  private void setReportList(List root) {
    Report report = null;
    for (int i = 0; i < root.size(); ++i) {
      report = new Report((Element)root.get(i));
      this.reportList.put(report.getId(), report);
    }
  }

  public void load() {
    setReportList(this.root.getChild("reportlist").getChildren("report"));

    C8_URL = this.root.getChild("cognos").getChildText("url");
    C8_NAMESPACE = this.root.getChild("cognos").getChildText("namespace");
    C8_USERNAME = this.root.getChild("cognos").getChildText("username");
    C8_PASSWORD = this.root.getChild("cognos").getChildText("password");
    C_VERSION = this.root.getChild("cognos").getChildText("version");
    REPORT_ROOT = this.root.getChildText("reportSaveRoot");

    MAIL_HOST = this.root.getChild("mailserver").getChildText("host");
    MAIL_USER = this.root.getChild("mailserver").getChildText("user");
    MAIL_PWD = this.root.getChild("mailserver").getChildText("password");
  }

  public String getFileName() {
    return this.fileName;
  }

  public void setFileName(String fileName) {
    this.fileName = fileName;
  }

  public Element getRoot() {
    return this.root;
  }

  public void setRoot(Element root) {
    this.root = root;
  }

  public Map<String, Report> getReportList() {
    return this.reportList;
  }

  public static String getC8_URL() {
    return C8_URL;
  }

  public static void setC8_URL(String c8_url) {
    C8_URL = c8_url;
  }

  public static String getC8_NAMESPACE() {
    return C8_NAMESPACE;
  }

  public static void setC8_NAMESPACE(String c8_namespace) {
    C8_NAMESPACE = c8_namespace;
  }

  public static String getC8_USERNAME() {
    return C8_USERNAME;
  }

  public static void setC8_USERNAME(String c8_username) {
    C8_USERNAME = c8_username;
  }

  public static String getC8_PASSWORD() {
    return C8_PASSWORD;
  }

  public static void setC8_PASSWORD(String c8_password) {
    C8_PASSWORD = c8_password;
  }

  public static String getREPORT_ROOT() {
    return REPORT_ROOT;
  }

  public static void setREPORT_ROOT(String report_root) {
    REPORT_ROOT = report_root;
  }

  public static String getMAIL_HOST() {
    return MAIL_HOST;
  }

  public static void setMAIL_HOST(String mail_host) {
    MAIL_HOST = mail_host;
  }

  public static String getMAIL_USER() {
    return MAIL_USER;
  }

  public static void setMAIL_USER(String mail_user) {
    MAIL_USER = mail_user;
  }

  public static String getMAIL_PWD() {
    return MAIL_PWD;
  }

  public static void setMAIL_PWD(String mail_pwd) {
    MAIL_PWD = mail_pwd;
  }

  public static void main(String[] args)
  {
    XmlReportsLoader report = new XmlReportsLoader();

    report.load();
    System.out.println(getC8_URL());
    System.out.println(getC8_NAMESPACE());
    System.out.println(getC8_USERNAME());
    System.out.println(getC8_PASSWORD());
    System.out.println(getREPORT_ROOT());
    System.out.println(getMAIL_HOST());
    System.out.println(getMAIL_USER());
    System.out.println(getMAIL_PWD());
    Map reports = report.getReportList();
    Report r = (Report)reports.get("5");
    System.out.println(r.getId());
    System.out.println(r.getSearchpath());
    System.out.println(r.getSavefolder());
    System.out.println(r.getCopyfolder());
    System.out.println(r.getPrefixFilename());
    System.out.println(r.getFileFormat());
    System.out.println(r.getMail().getFrom());
    System.out.println(r.getMail().getTo());
    System.out.println(r.getMail().getSubject());
    System.out.println(r.getMail().getContent());
    System.out.println(r.getMail().isAttachfile());
    System.out.println("------------------------------");
  }

public static String getC_VERSION() {
	return C_VERSION;
}

public static void setC_VERSION(String c_VERSION) {
	C_VERSION = c_VERSION;
}
}